package com.qzsf.backend.mapper;

import com.qzsf.backend.dto.EventDetailDTO;
import com.qzsf.backend.entity.Event;
import com.qzsf.backend.mapper.sql.EventSqlProvider;
import com.qzsf.backend.vo.UserEventVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/*
 * @Author: Dennis Chen
 * @CreateTime: 2024/11/17 19:24 周日
 * @Email 1429278462@qq.com
 * @Remark 活动数据访问层
 * @Version 1.0
 */
@Mapper
@Repository
public interface EventMapper {
    @SelectProvider(type = EventSqlProvider.class, method = "getEventsByCondition")
    List<EventDetailDTO> getEventsByCondition(Integer status, Integer typeId, Integer organizerId);

    @Delete("delete from event where id =#{id}")
    int deleteEvent(int id);

    @Update("update event set typeid = #{typeId},name = #{name},number = #{number},location = #{location},description = #{description},pics = #{pics},start_time = #{startTime},end_time = #{endTime},hours = #{hours},auth_id = #{authId} where id = #{id}")
    int updateEvent(Event event);

    @Insert("insert into event(typeid, name, number, remaining, location, description, pics, start_time, end_time, hours, auth_id)values (#{typeId}, #{name}, #{number}, #{number}, #{location}, #{description}, #{pics}, #{startTime}, #{endTime}, #{hours}, #{authId})")
    int createEvent(Event event);

    void updateRemaining();

    //获取活动的剩余人数
    @Select("SELECT remaining FROM event WHERE id = #{eventId}")
    int getRemainingByEventId(@Param("eventId") Integer eventId);

    @Select("select * from event where id  = #{id}")
    Event getEventById(int id);

    @Select("select * from event")
    List<Event> getEventList();

    @Select("select count(*) from event where end_time<now()")
    int getCompletedEvents();

    @Select("select count(*) from event where start_time>now()")
    int getUpcomingEvents();

    @Select("select id as eventId, name as eventName ,description as eventDescription,pics from event where end_time<now() order by end_time desc")
    List<UserEventVO> getPastEvent();

    @Select("select a.id, a.typeId, b.type as typeName, a.name, a.number, a.remaining, a.location, a.description, a.pics, a.start_time, a.end_time, a.hours, a.auth_id, c.name as organizerName,c.pic as organizerPic from event a LEFT JOIN event_type b ON a.typeId = b.id LEFT JOIN organizer_profile c ON a.auth_id = c.id where a.id = #{eventId}")
    EventDetailDTO getEventDetailMobile(int eventId);

    @Select("select * from event where auth_id = #{id} and (name like concat('%',#{query},'%') or description like concat('%',#{query},'%'))")
    List<Event> getOrganizerEventsByQuery(int id,String query);

    @Select("select * from event where auth_id = #{id}")
    List<Event> getOrganizerEvents(int id);

    @Update("update event set typeid = #{typeId},name = #{name},number = #{number},location = #{location},description = #{description},pics = #{pics},start_time = #{startTime},end_time = #{endTime},hours = #{hours} where id = #{id}")
    int updateOrganizerEvent(Event event);
}
